//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * buyguideDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.buyguide.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.buyguide.dao.IBuyguideDao;
import com.gamutsoft.amb.buyguide.model.Buyguide;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;

@SuppressWarnings("unchecked")
public class BuyguideDaoHibernate extends GenericDaoHibernate<Buyguide, Long> implements IBuyguideDao {
    
     public BuyguideDaoHibernate() {
		super(Buyguide.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchBuyguideList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.NAME,T.URL,T.PARENT_ID,T.CREATE_TIME,T.STATUS FROM BUYGUIDE T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Buyguide searchBuyguideById(Long id) throws BusinessException {
	
	  Buyguide domain = new Buyguide();
	   String hql = "from Buyguide where id=?";
	   List<Buyguide> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteBuyguideById(Long id) throws BusinessException {
	
	 String hql ="delete from Buyguide where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchBuyguideListByWhere(QueryExample qe, Buyguide domain)
			throws BusinessException {
		 StringBuffer sb = new StringBuffer();
		 sb.append("SELECT T.ID,T.NAME,T.URL,T.PARENT_ID, B.NAME PARENT_NAME,T.CREATE_TIME,T.STATUS FROM BUYGUIDE T");
		 sb.append(" LEFT JOIN BUYGUIDE B ON T.PARENT_ID=B.ID ");
		 if(domain!=null){
			 if(domain.getStatus()!=null && !"".equals(domain.getStatus())){
				 qe.addCondictions("T.STATUS","=",domain.getStatus());
			 }
			 if(domain.getParentId()!=null){
				 qe.addCondictions("T.PARENT_ID","=",domain.getParentId());
			 }
		 }
		 qe.addOrderBy(" T.CREATE_TIME desc ");
		  return this.findByQueryExample(sb.toString(), qe);
	}
	
}